<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );

class EcommerceController extends JController
{
	function display()
	{
		parent::display();
	}
	
	function carFavourite(){
        //xu ly favourite
        $db = &JFactory::getDBO();
        $user = JFactory::getUser();
        $temp = JRequest::getVar('carid');
		$strCar = implode("," ,$temp);
		$query = "SELECT like_products FROM #__users WHERE id = ".$user->id;
		$db->setQuery($query);
		$like_pro = $db->loadResult();
        if($like_pro == ''){ 
			$query = "UPDATE #__users SET like_products = '".$strCar."' WHERE id = ".$user->id;
			$db->setQuery($query);
			$db->query();
			$this->setRedirect($_SERVER['HTTP_REFERER'], 'Dette produkt blev tilføjet til listen');
        } else { 
			$arrPro = explode(",", $like_pro);
			foreach($temp as $id){
				if(in_array($id, $arrPro, TRUE)){
					$arr[] = $id;
				}
			}
			if($arr){
				$strPro = implode(',', $arr);
				$query = "SELECT model FROM #__pr_product WHERE id IN (".$strPro.")";
				$db->setQuery($query);
				$proModelArr = $db->loadResultArray();
				$strProModel = implode(', ', $proModelArr);
				$this->setRedirect($_SERVER['HTTP_REFERER'], $strProModel." allerede i listen", "error");
			} else {
				$string = $like_pro.','.$strCar;
				$q = "UPDATE #__users SET like_products = '".$string."' WHERE id = ".$user->id;
				$db->setQuery($q);
				$db->query();
				$this->setRedirect($_SERVER['HTTP_REFERER'], 'Dette produkt blev tilføjet til listen');
			}
		}
    }
	
	function delLike(){
		$db = &JFactory::getDBO();
		$user = JFactory::getUser();
		$id = JRequest::getVar('id');
		
		$query = "SELECT like_products FROM #__users WHERE id = ".$user->id;
		$db->setQuery($query);
		$like_pro = $db->loadResult();
		$new_str = str_replace($id.',', '', $like_pro, $count);
		if(!$count){
			$new_str = str_replace(','.$id, '', $like_pro, $count);
		}
		if(!$count){
			$new_str = str_replace($id, '', $like_pro, $count);
		}
		$query = "UPDATE #__users SET like_products = '".$new_str."' WHERE id = ".$user->id;
		$db->setQuery($query);
		$db->query();		
		$this->setRedirect($_SERVER['HTTP_REFERER'], 'Produkter blev fjernet fra listen');
		
	}
	
	function registerUser(){
		$sess = JFactory::getSession();
		if(JRequest::getVar('userid')){
			$sess->set('userid',JRequest::getVar('userid'));
			$sess->set('email',JRequest::getVar('email'));
			$sess->set('name',JRequest::getVar('name'));
		} else {
			//if(!$this->checkEmail(JRequest::getVar('email'))) return;
			
			$sess->set('email',JRequest::getVar('email'));
			$sess->set('name',JRequest::getVar('name'));
			$sess->set('company',JRequest::getVar('company'));
			$sess->set('address',JRequest::getVar('address'));
			$sess->set('code',JRequest::getVar('code'));
			$sess->set('city',JRequest::getVar('city'));
			$sess->set('phone',JRequest::getVar('phone'));
			$sess->set('cvr',JRequest::getVar('cvr'));
		}

		if(JRequest::getVar('at')){
			$this->setRedirect('index.php?option=com_ecommerce&view=register&Itemid=7&layout=atcompany');
		} else {
			$this->setRedirect('index.php?option=com_ecommerce&view=register&Itemid=7&layout=step1');
		}
	}
	
	function registerCar(){
		$sess = JFactory::getSession();
		$db = JFactory::getDBO();
		$this->checkImage("index.php?option=com_ecommerce&view=register&Itemid=7");
		
		if($sess->get('userid')){
			$userid = $sess->get('userid');
			$email 		= $sess->get('email');
			$name 		= $sess->get('name');
		} else {
			$email 		= $sess->get('email');
			$name 		= $sess->get('name');
			$address 	= $sess->get('address');
			$code 		= $sess->get('code');
			$city 		= $sess->get('city');
			$phone 		= $sess->get('phone');
			$cvr 		= $sess->get('cvr');
			$company 	= $sess->get('company');
			
			$query = "INSERT INTO #__users (email, name, address, code, city, phone, cvr, post, company) VALUES ('".$email."', '".$name."', '".$address."', '".$code."', '".$city."', '".$phone."', '".$cvr."', 1, '".$company."')";
			$db->setQuery($query);
			if(!$db->query()){
				echo '<script>alert("'.$db->getError().'");window.history.go(-1);</script>';
				exit();
			}
			$userid = mysql_insert_id();
		}

		$model = JRequest::getVar('model');
		$chassis = JRequest::getVar('chassis');
		$desired_price = JRequest::getVar('desired_price');
		$alias = JFilterOutput::stringURLSafe($model);
		$placering = JRequest::getVar('placering');
		$car_type = JRequest::getVar('car_type');
		$fuel = JRequest::getVar('fuel');
		$register_day = JRequest::getVar('register_day');
		$view_day = JRequest::getVar('view_day');
		$registry = JRequest::getVar('registry');
		$stand = JRequest::getVar('stand');
		$km = JRequest::getVar('km');
		$carid = JRequest::getVar('carid');
		$katno = rand(100,999);
		$comment = JRequest::getVar('comment');
		$video = JRequest::getVar('video');
		
		if(JRequest::getVar('abs')) 	$abs = 'ABS-bremser';
		if(JRequest::getVar('alarm')) 	$alarm = 'Alarm';
		if(JRequest::getVar('antispin')) $antispin = 'Antispin';
		if(JRequest::getVar('start')) 	$start = 'Startspærre';
		if(JRequest::getVar('esp')) 	$esp = 'ESP / ESC';
		if(JRequest::getVar('airbag')) 	$airbag = 'Airbags: '.JRequest::getVar('airbag');
		
		if(JRequest::getVar('armrest')) 	$armrest = 'Armlæn';
		if(JRequest::getVar('split_seat')) 	$split_seat = 'Splitbagsæde';
		if(JRequest::getVar('wood')) 		$wood = 'Træinteriør';
		if(JRequest::getVar('leather_steering')) $leather_steering = 'Læderrat';
		if(JRequest::getVar('seat_cover')) 	$seat_cover = 'Sædebetræk:'.JRequest::getVar('seat_cover');
		
		if(JRequest::getVar('air_con')) 		$air_con = 'Aircondition';
		if(JRequest::getVar('elc_heat_mirror')) $elc_heat_mirror = 'Elspejle med varme';
		if(JRequest::getVar('sensor')) 			$sensor = 'Parkeringssensor';
		if(JRequest::getVar('automatic')) 		$automatic = 'Automatgear';
		if(JRequest::getVar('elec_seat')) 		$leather_steering = 'Læderrat';
		if(JRequest::getVar('radio')) 			$radio = 'Radio med cd';
		if(JRequest::getVar('central')) 		$central = 'Centrallås';
		if(JRequest::getVar('cruise')) 			$cruise = 'Fartpilot';
		if(JRequest::getVar('servo')) 			$servo = 'Servo';
		if(JRequest::getVar('remote')) 			$remote = 'Centrallås fjernb.';
		if(JRequest::getVar('gps')) 			$servo = 'GPS-navigation';
		if(JRequest::getVar('sunroof')) 		$sunroof = 'Soltag';
		if(JRequest::getVar('power_window')) 	$power_window = 'Elruder';
		if(JRequest::getVar('air_con1')) 		$air_con1 = 'Klimaanlæg';
		if(JRequest::getVar('elec_sunroof')) 	$elec_sunroof = 'Soltag elektrisk';
		if(JRequest::getVar('elec_mirror')) 	$servo = 'Elsidespejle';
		if(JRequest::getVar('computer')) 		$computer = 'Kørecomputer';
		if(JRequest::getVar('heated_seat')) 	$heated_seat = 'Varme i sæder';
		if(JRequest::getVar('power_steering')) 	$power_steering = 'Servostyring';
		if(JRequest::getVar('bak_sensor')) 		$bak_sensor = 'Bak-sensor (PDC)';
		if(JRequest::getVar('elec_adj_seat')) 	$elec_adj_seat = 'El-indstillelige sæder';
		
		if(JRequest::getVar('towbar')) 			$towbar = 'Anhængertræk';
		if(JRequest::getVar('lower')) 			$lower = 'Sænket undervogn';
		if(JRequest::getVar('fog_light')) 		$fog_light = 'Tågelygter';
		if(JRequest::getVar('removable_towbar'))$removable_towbar = 'Anhæng. aftageligt';
		if(JRequest::getVar('xenon')) 			$xenon = 'Xenonlygter';
		if(JRequest::getVar('roof_rail')) 		$roof_rail = 'Tagræling';
		if(JRequest::getVar('lamp_washer')) 	$lamp_washer = 'Lygtevasker';
		if(JRequest::getVar('spoiler')) 		$spoiler = 'Spoiler';
		
		$prodir = "components/com_ecommerce/imgupload/";
		if($_FILES['image']['name']){
			$rand_image = mt_rand();
			$image = $rand_image.$_FILES['image']['name'];
			$desphoto = $prodir.$rand_image.$_FILES['image']['name'];
			move_uploaded_file($_FILES['image']['tmp_name'], $desphoto);
		} else $image = '';
		if($_FILES['image1']['name']){
			$rand_image1 = mt_rand();
			$image1 = $rand_image1.$_FILES['image1']['name'];
			$desphoto = $prodir.$rand_image1.$_FILES['image1']['name'];
			move_uploaded_file($_FILES['image1']['tmp_name'], $desphoto);
		} else $image1 = '';
		if($_FILES['image2']['name']){
			$rand_image2 = mt_rand();
			$image2 = $rand_image2.$_FILES['image2']['name'];
			$desphoto = $prodir.$rand_image2.$_FILES['image2']['name'];
			move_uploaded_file($_FILES['image2']['tmp_name'], $desphoto);
		} else $image2 = '';
		if($_FILES['image3']['name']){
			$rand_image3 = mt_rand();
			$image3 = $rand_image3.$_FILES['image3']['name'];
			$desphoto = $prodir.$rand_image3.$_FILES['image3']['name'];
			move_uploaded_file($_FILES['image3']['tmp_name'], $desphoto);
		} else $image3 = '';
		if($_FILES['image4']['name']){
			$rand_image4 = mt_rand();
			$image4 = $rand_image4.$_FILES['image4']['name'];
			$desphoto = $prodir.$rand_image4.$_FILES['image4']['name'];
			move_uploaded_file($_FILES['image4']['tmp_name'], $desphoto);
		} else $image4 = '';
		
		$num = JRequest::getVar('imageNum');
		if($num){
			for($i = 0; $i<$num; $i++){
				if($_FILES['imageadd'.$i]['name']){
					$rand = mt_rand();
					$nameArr[$i] = $rand.$_FILES['imageadd'.$i]['name'];
					$desphoto = $prodir.$rand.$_FILES['imageadd'.$i]['name'];
					move_uploaded_file($_FILES['imageadd'.$i]['tmp_name'], $desphoto);
				}
			}
		}
		
		$query = "INSERT INTO #__pr_product (alias, type, desired_price, model, register_day, car_type, view_day, registry, stand, km, fuel, comment, image, image1, image2, image3, image4, video, published, user_id, approve, bought, carid, katno, placering, chassis, abs, alarm, antispin, start, esp, airbag, armrest, split_seat, wood, leather_steering, seat_cover, air_con, elc_heat_mirror, sensor, automatic, elec_seat, radio, central, cruise, servo, remote, gps, sunroof, power_window, air_con1, elec_sunroof, elec_mirror, computer, heated_seat, power_steering, bak_sensor, elec_adj_seat, heated_mirror, towbar, lower, fog_light, removable_towbar, xenon, roof_rail, lamp_washer, spoiler) VALUES ('".$alias."', 1, '".$desired_price."', '".$model."', '".$register_day."', '".$car_type."', '".$view_day."', '".$registry."', '".$stand."', '".$km."', '".$fuel."', '".$comment."', '".$image."', '".$image1."', '".$image2."', '".$image3."', '".$image4."', '".$video."', 1, '".$userid."', 0, 0,'".$carid."', '".$katno."', '".$placering."', '".$chassis."', '".$abs."', '".$alarm."', '".$antispin."', '".$start."', '".$esp."', '".$airbag."', '".$armrest."', '".$split_seat."', '".$wood."', '".$leather_steering."', '".$seat_cover."', '".$air_con."', '".$elc_heat_mirror."', '".$sensor."', '".$automatic."', '".$elec_seat."', '".$radio."', '".$central."', '".$cruise."', '".$servo."', '".$remote."', '".$gps."', '".$sunroof."', '".$power_window."', '".$air_con1."', '".$elec_sunroof."', '".$elec_mirror."', '".$computer."', '".$heated_seat."', '".$power_steering."', '".$bak_sensor."', '".$elec_adj_seat."', '".$heated_mirror."', '".$towbar."', '".$lower."', '".$fog_light."', '".$removable_towbar."', '".$xenon."', '".$roof_rail."', '".$lamp_washer."', '".$spoiler."')";
		$db->setQuery($query);
		if(!$db->query()){
			echo '<script>alert("'.$db->getError().'");window.history.go(-1);</script>';
			exit();
		}
		$proid = $db->insertid();
		foreach($nameArr as $imgName){
			$query = "INSERT INTO #__pr_image (name, product_id) VALUES ('".$imgName."', ".$proid.")";
			$db->setQuery($query);
			$db->query();
		}
		$this->_sendMailUser($email, $name, $model);
		$this->_sendMailAdmin($name, $model);
		$this->clearSess1();
		$this->setRedirect('index.php?option=com_ecommerce&view=register&layout=success&Itemid=7');

	}
	function checkImage($link){
		if($_FILES['image']['name']){
			$ext = strtolower(str_replace(".", "", strrchr($_FILES['image']['name'], ".")));
			if(! in_array($ext,array('gif', 'jpg', 'jpeg', 'png', 'bmp')) ){
				$this->setRedirect($link,"Filen er ikke billede");
			}
			if($_FILES['image']['size'] > 2097152){
				$this->setRedirect($link,"Fil størrelse større end 2 MB");
			}
		}
		if($_FILES['image1']['name']){
			$ext = strtolower(str_replace(".", "", strrchr($_FILES['image1']['name'], ".")));
			if(! in_array($ext,array('gif', 'jpg', 'jpeg', 'png', 'bmp')) ){
				$this->setRedirect($link,"Filen er ikke billede");
			}
			if($_FILES['image1']['size'] > 2097152){
				$this->setRedirect($link,"Fil størrelse større end 2 MB");
			}
		}
		if($_FILES['image2']['name']){
			$ext = strtolower(str_replace(".", "", strrchr($_FILES['image2']['name'], ".")));
			if(! in_array($ext,array('gif', 'jpg', 'jpeg', 'png', 'bmp')) ){
				$this->setRedirect($link,"Filen er ikke billede");
			}
			if($_FILES['image2']['size'] > 2097152){
				$this->setRedirect($link,"Fil størrelse større end 2 MB");
			}
		}
		if($_FILES['image3']['name']){
			$ext = strtolower(str_replace(".", "", strrchr($_FILES['image3']['name'], ".")));
			if(! in_array($ext,array('gif', 'jpg', 'jpeg', 'png', 'bmp')) ){
				$this->setRedirect($link,"Filen er ikke billede");
			}
			if($_FILES['image3']['size'] > 2097152){
				$this->setRedirect($link,"Fil størrelse større end 2 MB");
			}
		}
	}
	
	function saveStep1(){
		$sess = JFactory::getSession();
		
		$sess->set('car_type',JRequest::getVar('car_type'));
		$sess->set('placering',JRequest::getVar('placering'));
		$sess->set('model',JRequest::getVar('model'));
		$sess->set('fuel',JRequest::getVar('fuel'));
		$sess->set('desired_price',JRequest::getVar('desired_price'));
		$sess->set('register_day',JRequest::getVar('register_day'));
		$sess->set('view_day',JRequest::getVar('view_day'));
		$sess->set('registry',JRequest::getVar('registry'));
		$sess->set('stand',JRequest::getVar('stand'));
		$sess->set('km',JRequest::getVar('km'));
		$sess->set('color',JRequest::getVar('color'));
		$sess->set('color_type',JRequest::getVar('color_type'));
		$sess->set('cabin_color',JRequest::getVar('cabin_color'));
		$sess->set('chassis',JRequest::getVar('chassis'));
		$sess->set('carid',JRequest::getVar('carid'));
		
		if(JRequest::getVar('action')){
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step5");
		} else {
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step2");
		}
		
	}
	
	function saveStep2(){
		$sess = JFactory::getSession();
		
		if(JRequest::getVar('abs')) 		$sess->set('abs','ABS-bremser');	else $sess->clear('abs');
		if(JRequest::getVar('alarm')) 		$sess->set('alarm','Alarm');		else $sess->clear('alarm');
		if(JRequest::getVar('antispin')) 	$sess->set('antispin','Antispin');	else $sess->clear('antispin');
		if(JRequest::getVar('start')) 		$sess->set('start','Startspærre');	else $sess->clear('start');
		if(JRequest::getVar('esp')) 		$sess->set('esp','ESP / ESC');		else $sess->clear('esp');
		if(JRequest::getVar('airbag')) 		$sess->set('airbag',JRequest::getVar('airbag'));
		
		if(JRequest::getVar('armrest')) 	$sess->set('armrest','Armlæn');			else $sess->clear('armrest');
		if(JRequest::getVar('split_seat')) 	$sess->set('split_seat','Splitbagsæde');else $sess->clear('split_seat');
		if(JRequest::getVar('wood')) 		$sess->set('wood','Træinteriør');			else $sess->clear('wood');
		if(JRequest::getVar('leather_steering')) $sess->set('leather_steering','Læderrat');else $sess->clear('leather_steering');
		if(JRequest::getVar('seat_cover')) 	$sess->set('seat_cover',JRequest::getVar('seat_cover'));
		if(JRequest::getVar('flexible')) 	$sess->set('flexible',JRequest::getVar('flexible'));
		if(JRequest::getVar('stand_tire')){	$sess->set('stand_tire',JRequest::getVar('stand_tire'));$sess->set('alu_tire','');}
		if(JRequest::getVar('alu_tire')){ 	$sess->set('alu_tire',JRequest::getVar('alu_tire'));$sess->set('stand_tire','');}
		if(JRequest::getVar('tread')) 		$sess->set('tread',JRequest::getVar('tread'));
		
		if(JRequest::getVar('air_con')) 	$sess->set('air_con','Aircondition');	else $sess->clear('air_con');
		if(JRequest::getVar('elc_heat_mirror')) 	$sess->set('elc_heat_mirror','Elspejle med varme'); else $sess->clear('elc_heat_mirror');
		if(JRequest::getVar('sensor')) 		$sess->set('sensor','Parkeringssensor'); else $sess->clear('sensor');
		if(JRequest::getVar('automatic')) 	$sess->set('automatic','Automatgear');	else $sess->clear('automatic');
		if(JRequest::getVar('elec_seat')) 	$sess->set('elec_seat','Elsæder');		else $sess->clear('elec_seat');
		if(JRequest::getVar('radio')) 		$sess->set('radio','Radio med cd');		else $sess->clear('radio');
		if(JRequest::getVar('central')) 	$sess->set('central','Centrallås');		else $sess->clear('central');
		if(JRequest::getVar('cruise')) 		$sess->set('cruise','Fartpilot');		else $sess->clear('cruise');
		if(JRequest::getVar('servo')) 		$sess->set('servo','Servo');			else $sess->clear('servo');
		if(JRequest::getVar('remote')) 		$sess->set('remote','Centrallås fjernb.');else $sess->clear('remote');
		if(JRequest::getVar('gps')) 		$sess->set('gps','GPS-navigation');else $sess->clear('gps');
		if(JRequest::getVar('sunroof')) 	$sess->set('sunroof','Soltag');else $sess->clear('sunroof');
		if(JRequest::getVar('power_window')) 	$sess->set('power_window','Elruder');else $sess->clear('power_window');
		if(JRequest::getVar('air_con1')) 	$sess->set('air_con1','Klimaanlæg');else $sess->clear('air_con1');
		if(JRequest::getVar('elec_sunroof')) 	$sess->set('elec_sunroof','Soltag elektrisk');else $sess->clear('elec_sunroof');
		if(JRequest::getVar('elec_mirror')) $sess->set('elec_mirror','Elsidespejle');else $sess->clear('elec_mirror');
		if(JRequest::getVar('computer')) 	$sess->set('computer','Kørecomputer');else $sess->clear('computer');
		if(JRequest::getVar('heated_seat')) $sess->set('heated_seat','Varme i sæder');else $sess->clear('heated_seat');
		if(JRequest::getVar('power_steering')) 	$sess->set('power_steering','Servostyring');else $sess->clear('power_steering');
		if(JRequest::getVar('bak_sensor')) 	$sess->set('bak_sensor','Bak-sensor (PDC)');else $sess->clear('bak_sensor');
		if(JRequest::getVar('elec_adj_seat')) 	$sess->set('elec_adj_seat','El-indstillelige sæder');else $sess->clear('elec_adj_seat');
		if(JRequest::getVar('heated_mirror')) 	$sess->set('heated_mirror',JRequest::getVar('heated_mirror'));else $sess->clear('heated_mirror');
		
		if(JRequest::getVar('towbar')) 		$sess->set('towbar','Anhængertræk');else $sess->clear('towbar');
		if(JRequest::getVar('lower'))	 	$sess->set('lower','Sænket undervogn');else $sess->clear('lower');
		if(JRequest::getVar('fog_light')) 	$sess->set('fog_light','Tågelygter');else $sess->clear('fog_light');
		if(JRequest::getVar('removable_towbar')) 	$sess->set('removable_towbar','Anhæng. aftageligt');else $sess->clear('removable_towbar');
		if(JRequest::getVar('xenon')) 		$sess->set('xenon','Xenonlygter');else $sess->clear('xenon');
		if(JRequest::getVar('roof_rail')) 	$sess->set('roof_rail','Tagræling');else $sess->clear('roof_rail');
		if(JRequest::getVar('lamp_washer')) $sess->set('lamp_washer','Lygtevasker');else $sess->clear('lamp_washer');
		if(JRequest::getVar('spoiler')) 	$sess->set('spoiler','Spoiler');else $sess->clear('spoiler');
		
		if(JRequest::getVar('action')){
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step5");
		} else {
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step3");
		}
	}
	
	function saveStep3(){
		$sess = JFactory::getSession();
		if(JRequest::getVar('clean')) 		$sess->set('clean',JRequest::getVar('clean'));
		if(JRequest::getVar('clean_content')) 		$sess->set('clean_content',JRequest::getVar('clean_content'));
		if(JRequest::getVar('interior_defect')) 	$sess->set('interior_defect',JRequest::getVar('interior_defect'));else $sess->clear('interior_defect');
		if(JRequest::getVar('external')) 	$sess->set('external',JRequest::getVar('external'));
		if(JRequest::getVar('front')) 		$sess->set('front',JRequest::getVar('front'));else $sess->clear('front');
		if(JRequest::getVar('rear')) 		$sess->set('rear',JRequest::getVar('rear'));else $sess->clear('rear');
		if(JRequest::getVar('left')) 		$sess->set('left',JRequest::getVar('left'));else $sess->clear('left');
		if(JRequest::getVar('right')) 		$sess->set('right',JRequest::getVar('right'));else $sess->clear('right');
		
		if(JRequest::getVar('action')){
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step5");
		} else {
			$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step4");
		}
	}
	
	function saveStep4(){
		$sess = JFactory::getSession();
		$this->checkImage("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step4");
		
		if(JRequest::getVar('machinal_defect')) 	$sess->set('machinal_defect',JRequest::getVar('machinal_defect'));else $sess->clear('machinal_defect');
		if(JRequest::getVar('electrical_defect')) 	$sess->set('electrical_defect',JRequest::getVar('electrical_defect'));else $sess->clear('electrical_defect');
		if(JRequest::getVar('service_book')) 		$sess->set('service_book',JRequest::getVar('service_book'));else $sess->clear('service_book');
		if(JRequest::getVar('belt')) 			$sess->set('belt',JRequest::getVar('belt'));else $sess->clear('belt');
		if(JRequest::getVar('front_brake')) 	$sess->set('front_brake','Forreste bremser skiftet indenfor de seneste kørte 10.000 km');
		if(JRequest::getVar('rear_brake')) 		$sess->set('rear_brake','Bagerste bremser skiftet indenfor de seneste kørte 10.000 km');
		if(JRequest::getVar('exhaust')) 		$sess->set('exhaust','Udstødning udskiftet indenfor de seneste kørte 10.000 km');
		if(JRequest::getVar('repainted')) 		$sess->set('repainted',JRequest::getVar('repainted'));else $sess->clear('repainted');
		if(JRequest::getVar('parking_damage')) 	$sess->set('parking_damage',JRequest::getVar('parking_damage'));else $sess->clear('parking_damage');
		if(JRequest::getVar('accident')) 		$sess->set('accident',JRequest::getVar('accident'));else $sess->clear('accident');
		if(JRequest::getVar('video')) 		$sess->set('video',JRequest::getVar('video'));else $sess->clear('video');
		
		$prodir = "components/com_ecommerce/imgupload/";
		if($_FILES['image']['name']){
			$rand_image = mt_rand();
			$sess->set('image',$rand_image.$_FILES['image']['name']);
			$desphoto = $prodir.$rand_image.$_FILES['image']['name'];
			move_uploaded_file($_FILES['image']['tmp_name'], $desphoto);
		}
		if($_FILES['image1']['name']){
			$rand_image1 = mt_rand();
			$sess->set('image1',$rand_image1.$_FILES['image1']['name']);
			$desphoto = $prodir.$rand_image1.$_FILES['image1']['name'];
			move_uploaded_file($_FILES['image1']['tmp_name'], $desphoto);
		}
		if($_FILES['image2']['name']){
			$rand_image2 = mt_rand();
			$sess->set('image2',$rand_image2.$_FILES['image2']['name']);
			$desphoto = $prodir.$rand_image2.$_FILES['image2']['name'];
			move_uploaded_file($_FILES['image2']['tmp_name'], $desphoto);
		}
		if($_FILES['image3']['name']){
			$rand_image3 = mt_rand();
			$sess->set('image3',$rand_image3.$_FILES['image3']['name']);
			$desphoto = $prodir.$rand_image3.$_FILES['image3']['name'];
			move_uploaded_file($_FILES['image3']['tmp_name'], $desphoto);
		}
		if($_FILES['image4']['name']){
			$rand_image4 = mt_rand();
			$sess->set('image4',$rand_image4.$_FILES['image4']['name']);
			$desphoto = $prodir.$rand_image4.$_FILES['image4']['name'];
			move_uploaded_file($_FILES['image4']['tmp_name'], $desphoto);
		}
		
		$num = JRequest::getVar('imageNum');
		if($num){
			for($i = 0; $i<$num; $i++){
				if($_FILES['imageadd'.$i]['name']){
					$rand = mt_rand();
					$nameArr[$i] = $rand.$_FILES['imageadd'.$i]['name'];
					$desphoto = $prodir.$rand.$_FILES['imageadd'.$i]['name'];
					move_uploaded_file($_FILES['imageadd'.$i]['tmp_name'], $desphoto);
				}
			}
			$sess->set('nameArr',$nameArr);
		}
		
		$this->setRedirect("index.php?option=com_ecommerce&view=register&Itemid=7&layout=step5");
	}
	
	function saveAll(){
		$sess = JFactory::getSession();
		$db = JFactory::getDBO();
		
		if($sess->get('userid')){
			$userid = $sess->get('userid');
			$email 		= $sess->get('email');
			$name 		= $sess->get('name');
		} else {
			$email 		= $sess->get('email');
			$name 		= $sess->get('name');
			$address 	= $sess->get('address');
			$code 		= $sess->get('code');
			$city 		= $sess->get('city');
			$phone 		= $sess->get('phone');
			$cvr 		= $sess->get('cvr');
			$company 	= $sess->get('company');
			
			$query = "INSERT INTO #__users (email, name, address, code, city, phone, cvr, post, company) VALUES ('".$email."', '".$name."', '".$address."', '".$code."', '".$city."', '".$phone."', '".$cvr."', 1, '".$company."')";
			$db->setQuery($query);
			if(!$db->query()){
				echo '<script>alert("'.$db->getError().'");window.history.go(-1);</script>';
				exit();
			}
			$userid = mysql_insert_id();
		}
		
		$car_type 		= $sess->get('car_type');
		$placering 		= $sess->get('placering');
		$model 			= $sess->get('model');
		$alias 			= JFilterOutput::stringURLSafe($model);
		$fuel 			= $sess->get('fuel');
		$desired_price	= $sess->get('desired_price');
		$register_day 	= $sess->get('register_day');
		$view_day 		= $sess->get('view_day');
		$registry 		= $sess->get('registry');
		$stand 			= $sess->get('stand');
		$km 			= $sess->get('km');
		$color 			= $sess->get('color');
		$color_type 	= $sess->get('color_type');
		$cabin_color 	= $sess->get('cabin_color');
		$chassis 		= $sess->get('chassis');
		$carid 			= $sess->get('carid');
		$katno 			= rand(100,999);
		
		$abs 			= $sess->get('abs');
		$alarm 			= $sess->get('alarm');
		$antispin 		= $sess->get('antispin');
		$start 			= $sess->get('start');
		$esp 			= $sess->get('esp');
		$airbag 		= $sess->get('airbag');
		
		$armrest 		= $sess->get('armrest');
		$split_seat 	= $sess->get('split_seat');
		$wood 			= $sess->get('wood');
		$leather_steering = $sess->get('leather_steering');
		$seat_cover 	= $sess->get('seat_cover');
		$flexible 		= $sess->get('flexible');
		$stand_tire 	= $sess->get('stand_tire');
		$alu_tire 		= $sess->get('alu_tire');
		$tread 			= $sess->get('tread');
		
		$air_con 		= $sess->get('air_con');
		$elc_heat_mirror = $sess->get('elc_heat_mirror');
		$sensor 		= $sess->get('sensor');
		$automatic 		= $sess->get('automatic');
		$elec_seat 		= $sess->get('elec_seat');
		$radio 			= $sess->get('radio');
		$central		= $sess->get('central');
		$cruise			= $sess->get('cruise');
		$servo			= $sess->get('servo');
		$remote			= $sess->get('remote');
		$gps			= $sess->get('gps');
		$sunroof		= $sess->get('sunroof');
		$power_window	= $sess->get('power_window');
		$air_con1		= $sess->get('air_con1');
		$elec_sunroof	= $sess->get('elec_sunroof');
		$elec_mirror	= $sess->get('elec_mirror');
		$computer		= $sess->get('computer');
		$heated_seat	= $sess->get('heated_seat');
		$power_steering = $sess->get('power_steering');
		$bak_sensor		= $sess->get('bak_sensor');
		$elec_adj_seat	= $sess->get('elec_adj_seat');
		$heated_mirror	= $sess->get('heated_mirror');
		
		$towbar 		= $sess->get('towbar');
		$lower			= $sess->get('lower');
		$fog_light		= $sess->get('fog_light');
		$removable_towbar = $sess->get('removable_towbar');
		$xenon			= $sess->get('xenon');
		$roof_rail		= $sess->get('roof_rail');
		$lamp_washer	= $sess->get('lamp_washer');
		$spoiler		= $sess->get('spoiler');
		
		$clean			= $sess->get('clean');
		$clean_content	= $sess->get('clean_content');
		$interior_defect = $sess->get('interior_defect');
		$external		= $sess->get('external');
		$front			= $sess->get('front');
		$rear			= $sess->get('rear');
		$left			= $sess->get('left');
		$right			= $sess->get('right');
		
		$machinal_defect 	= $sess->get('machinal_defect');
		$electrical_defect	= $sess->get('electrical_defect');
		$service_book		= $sess->get('service_book');
		$belt 				= $sess->get('belt');
		$front_brake		= $sess->get('front_brake');
		$rear_brake			= $sess->get('rear_brake');
		$exhaust			= $sess->get('exhaust');
		$repainted			= $sess->get('repainted');
		$parking_damage		= $sess->get('parking_damage');
		$accident			= $sess->get('accident');
		$video				= $sess->get('video');
		
		$image = $sess->get('image');
		$image1 = $sess->get('image1');
		$image2 = $sess->get('image2');
		$image3 = $sess->get('image3');
		$image4 = $sess->get('image4');
		
		$query = "INSERT INTO #__pr_product (alias, type, desired_price, model, register_day, car_type, view_day, registry, stand, km, fuel, comment, image, image1, image2, image3, image4, published, user_id, approve, bought, carid, katno, placering, color, color_type, cabin_color, chassis, abs, alarm, antispin, start, esp, airbag, armrest, split_seat, wood, leather_steering, seat_cover, flexible, stand_tire, alu_tire, tread, air_con, elc_heat_mirror, sensor, automatic, elec_seat, radio, central, cruise, servo, remote, gps, sunroof, power_window, air_con1, elec_sunroof, elec_mirror, computer, heated_seat, power_steering, bak_sensor, elec_adj_seat, heated_mirror, towbar, lower, fog_light, removable_towbar, xenon, roof_rail, lamp_washer, spoiler, clean, clean_content, interior_defect, external, front, rear, left_err, right_err, machinal_defect, electrical_defect, service_book, belt, front_brake, rear_brake, exhaust, repainted, parking_damage, accident, video) VALUES ('".$alias."', 1, '".$desired_price."', '".$model."', '".$register_day."', '".$car_type."', '".$view_day."', '".$registry."', '".$stand."', '".$km."', '".$fuel."', '".$comment."', '".$image."', '".$image1."', '".$image2."', '".$image3."', '".$image4."', 1, '".$userid."', 0, 0,'".$carid."', '".$katno."', '".$placering."', '".$color."', '".$color_type."', '".$cabin_color."', '".$chassis."', '".$abs."', '".$alarm."', '".$antispin."', '".$start."', '".$esp."', '".$airbag."', '".$armrest."', '".$split_seat."', '".$wood."', '".$leather_steering."', '".$seat_cover."', '".$flexible."', '".$stand_tire."', '".$alu_tire."', '".$tread."', '".$air_con."', '".$elc_heat_mirror."', '".$sensor."', '".$automatic."', '".$elec_seat."', '".$radio."', '".$central."', '".$cruise."', '".$servo."', '".$remote."', '".$gps."', '".$sunroof."', '".$power_window."', '".$air_con1."', '".$elec_sunroof."', '".$elec_mirror."', '".$computer."', '".$heated_seat."', '".$power_steering."', '".$bak_sensor."', '".$elec_adj_seat."', '".$heated_mirror."', '".$towbar."', '".$lower."', '".$fog_light."', '".$removable_towbar."', '".$xenon."', '".$roof_rail."', '".$lamp_washer."', '".$spoiler."', '".$clean."', '".$clean_content."', '".$interior_defect."', '".$external."', '".$front."', '".$rear."', '".$left."', '".$right."', '".$machinal_defect."', '".$electrical_defect."', '".$service_book."', '".$belt."', '".$front_brake."', '".$rear_brake."', '".$exhaust."', '".$repainted."', '".$parking_damage."', '".$accident."', '".$video."')";
		$db->setQuery($query);
		if(!$db->query()){
			echo '<script>alert("'.$db->getError().'");window.history.go(-1);</script>';
			exit();
		}
		$proid = $db->insertid();
		$nameArr = $sess->get('nameArr');
		foreach($nameArr as $imgName){
			$query = "INSERT INTO #__pr_image (name, product_id) VALUES ('".$imgName."', ".$proid.")";
			$db->setQuery($query);
			$db->query();
		}
		$this->_sendMailUser($email, $name, $model);
		$this->_sendMailAdmin($name, $model);
		$this->clearSess1();
		$this->clearSess2();
		$this->setRedirect('index.php?option=com_ecommerce&view=register&layout=success&Itemid=7');
	}
	
	function destroyRegister(){
		$sess = JFactory::getSession();
		$image = $sess->get('image');
		$image1 = $sess->get('image1');
		$image2 = $sess->get('image2');
		$image3 = $sess->get('image3');
		$image4 = $sess->get('image4');
		$nameArr = $sess->get('nameArr');
		$prodir = "components/com_ecommerce/imgupload/";
		unlink($prodir.$image);
		unlink($prodir.$image1);
		unlink($prodir.$image2);
		unlink($prodir.$image3);
		unlink($prodir.$image4);
		foreach($nameArr as $image){
			unlink($prodir . $image);
		}
		$this->clearSess1();
		$this->clearSess2();
		$this->setRedirect('index.php?option=com_ecommerce&view=register&Itemid=7');
	}
	function des(){
		$sess = JFactory::getSession();
		$user = JFactory::getUser();
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__session WHERE username = '".$user->username."'";
		$db->setQuery($query);
		$userSess = $db->loadObject();
		$sess->destroy();
		$query = "INSERT INTO #__session VALUES ('".$userSess->username."', '".$userSess->time."', '".$userSess->session_id."', ".$userSess->guest.", ".$userSess->userid.", '".$userSess->usertype."', ".$userSess->gid.", ".$userSess->client_id.", '".$userSess->data."')";
		$db->setQuery($query);
		$db->query();
		$this->setRedirect('index.php?option=com_ecommerce&view=register&Itemid=7');
	}
	function checkEmail($email){
		$db = JFactory::getDBO();
		$query = "SELECT id FROM #__users WHERE email = '".$email."'";
		$db->setQuery($query);
		if($db->loadResult()){
			$this->setRedirect('login-online-auktion.html','Din email er registeret, du kan nu login');
			return false;
		}
		return true;
	}
	function _sendMailUser($email, $name, $model){
		global $mainframe;

		$db		=& JFactory::getDBO();

		$sitename 		= $mainframe->getCfg( 'sitename' );
		$mailfrom 		= $mainframe->getCfg( 'mailfrom' );
		$fromname 		= $mainframe->getCfg( 'fromname' );
		$siteURL		= JURI::base();

		$subject 	= $sitename.' - Køretøjsregistrering';

		$message = 'Hej '.$name.',<br /><br />Din "'.$model.'" er hermed registret og tilmeldt. Vi vil behandle din tilmelding hurtigst muligt<br /><br />Med venlig hilsen,<br />Dansk Automobil Børs';

		//get all super administrator
		$query = 'SELECT name, email, sendEmail' .
				' FROM #__users' .
				' WHERE LOWER( usertype ) = "super administrator"';
		$db->setQuery( $query );
		$rows = $db->loadObjectList();

		// Send email to user
		if ( ! $mailfrom  || ! $fromname ) {
			$fromname = $rows[0]->name;
			$mailfrom = $rows[0]->email;
		}

		JUtility::sendMail($mailfrom, $fromname, $email, $subject, $message, true);
	}
	function _sendMailAdmin($name, $model){
		global $mainframe;

		$db		=& JFactory::getDBO();

		$sitename 		= $mainframe->getCfg( 'sitename' );
		$mailfrom 		= $mainframe->getCfg( 'mailfrom' );
		$fromname 		= $mainframe->getCfg( 'fromname' );
		$siteURL		= JURI::base();

		$subject 	= $sitename.' - Brugertilmeldning';

		$message = 'Hej admin,<br /><br />'.$name.' har tilmeldt "'.$model.'"<br /><br />Med venlig hilsen,<br />Dansk Automobil Børs';

		//get all super administrator
		$query = 'SELECT name, email, sendEmail' .
				' FROM #__users' .
				' WHERE LOWER( usertype ) = "super administrator"';
		$db->setQuery( $query );
		$rows = $db->loadObjectList();

		// Send email to user
		if ( ! $mailfrom  || ! $fromname ) {
			$fromname = $rows[0]->name;
			$mailfrom = $rows[0]->email;
		}
		foreach ( $rows as $row )
		{
			JUtility::sendMail($mailfrom, $fromname, $row->email, $subject, $message, true);
		}
	}
	
	function clearSess1(){
		$sess = JFactory::getSession();
		$sess->clear('email');
		$sess->clear('name');
		$sess->clear('address');
		$sess->clear('code');
		$sess->clear('city');
		$sess->clear('phone');
		$sess->clear('cvr');
		$sess->clear('company');
	}
	
	function clearSess2(){
		$sess = JFactory::getSession();
		$sess->clear('car_type');
		$sess->clear('placering');
		$sess->clear('model');
		$sess->clear('fuel');
		$sess->clear('desired_price');
		$sess->clear('register_day');
		$sess->clear('view_day');
		$sess->clear('registry');
		$sess->clear('stand');
		$sess->clear('km');
		$sess->clear('color');
		$sess->clear('color_type');
		$sess->clear('cabin_color');
		$sess->clear('chassis');
		$sess->clear('carid');
		
		$sess->clear('abs');
		$sess->clear('alarm');
		$sess->clear('antispin');
		$sess->clear('start');
		$sess->clear('esp');
		$sess->clear('airbag');
		
		$sess->clear('armrest');
		$sess->clear('split_seat');
		$sess->clear('wood');
		$sess->clear('leather_steering');
		$sess->clear('seat_cover');
		$sess->clear('flexible');
		$sess->clear('stand_tire');
		$sess->clear('alu_tire');
		$sess->clear('tread');
		
		$sess->clear('air_con');
		$sess->clear('elc_heat_mirror');
		$sess->clear('sensor');
		$sess->clear('automatic');
		$sess->clear('elec_seat');
		$sess->clear('radio');
		$sess->clear('central');
		$sess->clear('cruise');
		$sess->clear('servo');
		$sess->clear('remote');
		$sess->clear('gps');
		$sess->clear('sunroof');
		$sess->clear('power_window');
		$sess->clear('air_con1');
		$sess->clear('elec_sunroof');
		$sess->clear('elec_mirror');
		$sess->clear('computer');
		$sess->clear('heated_seat');
		$sess->clear('power_steering');
		$sess->clear('bak_sensor');
		$sess->clear('elec_adj_seat');
		$sess->clear('heated_mirror');
		
		$sess->clear('towbar');
		$sess->clear('lower');
		$sess->clear('fog_light');
		$sess->clear('removable_towbar');
		$sess->clear('xenon');
		$sess->clear('roof_rail');
		$sess->clear('lamp_washer');
		$sess->clear('spoiler');
		
		$sess->clear('clean');
		$sess->clear('clean_content');
		$sess->clear('interior_defect');
		$sess->clear('external');
		$sess->clear('front');
		$sess->clear('rear');
		$sess->clear('left');
		$sess->clear('right');
		
		$sess->clear('machinal_defect');
		$sess->clear('electrical_defect');
		$sess->clear('service_book');
		$sess->clear('belt');
		$sess->clear('front_brake');
		$sess->clear('rear_brake');
		$sess->clear('exhaust');
		$sess->clear('repainted');
		$sess->clear('parking_damage');
		$sess->clear('accident');
		$sess->clear('video');
		
		$sess->clear('image');
		$sess->clear('image1');
		$sess->clear('image2');
		$sess->clear('image3');
	}
	
	function loadUserData(){
		$db = JFactory::getDBO();
		$email = JRequest::getVar('email');
		$query = "SELECT * FROM #__users WHERE email = '".$email."'";
		$db->setQuery($query);
		if($user = $db->loadObject()){
			echo json_encode(array('status' => 1, 'name' => $user->name, 'company' => $user->company, 'cvr' => $user->cvr, 'address' => $user->address, 'code' => $user->code, 'city' => $user->city, 'phone' => $user->phone, 'userid' => $user->id, TRUE));
		} else {
			echo json_encode(array('status' => 0, TRUE));
		}
		exit;
	}
}
?>